<template>
  <div class="page-container">
    <!--工具栏-->
    <div class="toolbar" style="float:left;padding-top:10px;padding-left:15px;">
      <el-form :inline="true" :model="filters" :size="size" ref="filters">
        <el-form-item prop="serveno">
          <el-input v-model="filters.serveno" placeholder="换货单编号"></el-input>
        </el-form-item>
        <el-form-item prop="username">
          <el-input v-model="filters.username" placeholder="换货申请人"></el-input>
        </el-form-item>
        <el-form-item prop="shopid">
          <el-select v-model="filters.shopid" placeholder="所属店铺">
            <el-option v-for="item in shopList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-row>
          <el-form-item>
            <kt-button
              icon="fa fa-search"
              :label="$t('action.search')"
              type="primary"
              @click="findPage(null)"
            />
          </el-form-item>
          <el-form-item>
            <kt-button icon="fa fa-refresh" :label="$t('action.reset')" type="primary" @click="resetForm('filters')"/>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item>
            <kt-button
              label="换货单处理"
              type="active"
              @click="handleEdit"
            />
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div class="toolbar" style="float:right;padding-top:10px;padding-right:15px;">
      <el-form :inline="true" :size="size">
        <el-form-item>
          <el-button-group>
            <el-tooltip content="刷新" placement="top">
              <el-button icon="fa fa-refresh" @click="findPage(null)"></el-button>
            </el-tooltip>
            <el-tooltip content="列显示" placement="top">
              <el-button icon="fa fa-filter" @click="displayFilterColumnsDialog"></el-button>
            </el-tooltip>
            <el-tooltip content="导出" placement="top">
              <el-button icon="fa fa-file-excel-o" @click="downloadExcel"></el-button>
            </el-tooltip>
          </el-button-group>
        </el-form-item>
      </el-form>
      <!--表格显示列界面-->
      <table-column-filter-dialog
        ref="tableColumnFilterDialog"
        :columns="columns"
        @handleFilterColumns="handleFilterColumns"
      ></table-column-filter-dialog>
    </div>
    <!--表格内容栏-->
    <cy-table
      :height="350"
      :data="pageResult"
      :columns="filterColumns"
      @findPage="findPage"
      @selectionChange="selectionChange"
      @handleDelete="handleDelete"
      :showEditOperation="false"
      ref="CyTable"
    ></cy-table>
    <!--新增编辑界面-->
    <el-dialog
      title="换货处理"
      width="40%"
      :visible.sync="editDialogVisible"
      :close-on-click-modal="false"
    >
      <el-form
        :model="dataForm"
        label-width="80px"
        :rules="dataFormRules"
        ref="dataForm"
        :size="size"
      >
        <el-form-item label="id" prop="id" v-if="false">
          <el-input v-model="dataForm.id" :disabled="true" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="换货服务编号" class="styleReplace"  prop="serveno">
          <el-input v-model="dataForm.serveno" auto-complete="off" :disabled="true"  ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status" class="styleReplace">
          <el-select v-model="dataForm.status" placeholder="请选择状态" clearable style="width:100%">
            <el-option v-for="item in statusList" :key="item.id" :label="item.val" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align:center">
        <el-button :size="size" @click.native="editDialogVisible = false">{{$t('action.cancel')}}</el-button>
        <el-button
          :size="size"
          type="primary"
          @click.native="submitForm"
          :loading="editLoading"
        >{{$t('action.submit')}}
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import CyTable from "@/views/Core/CyTable";
  import KtButton from "@/views/Core/KtButton";
  import {format} from "@/utils/datetime";
  import TableColumnFilterDialog from "@/views/Core/TableColumnFilterDialog";

  export default {
    components: {
      CyTable,
      KtButton,
      TableColumnFilterDialog
    },
    data() {
      return {
        size: "small",
        filters: {
          shopid: "",
          serveno: "",
          username: "",
          t: "changeProduct"
        },
        columns: [
          {prop: "id", label: "序号", minWidth: 50},
          {prop: "serveno", label: "服务(换货)编号", minWidth: 120},
          {prop: "pronumber", label: "原订单编号", minWidth: 120},
          {prop: "number", label: "新订单编号", minWidth: 120},
          {prop: "newproductname", label: "换货商品", minWidth: 80},
          {prop: "productnum", label: "换货商品数量", minWidth: 120},
          {prop: "username", label: "换货申请人", minWidth: 120},
          {prop: "proordermoney", label: "原订单总金额", minWidth: 100},
          {prop: "ordermoney", label: "新订单总金额", minWidth: 100},
          {prop: "statusname", label: "状态", minWidth: 80},
          {prop: "create_time", label: "创建时间", minWidth: 120}
        ],
        filterColumns: [],
        pageRequest: {pageNum: 1, pageSize: 10},
        pageResult: {},

        operation: false, // true:新增, false:编辑
        editDialogVisible: false, // 新增编辑界面是否显示
        editLoading: false,
        dataFormRules: {
          replyContent: [{required: true, message: "回复内容", trigger: "blur"}]
        },
        // 新增编辑界面数据
        dataForm: {
          serveno: "",
          status: ""
        },
        shopList: [],
        statusList: [],
        checked: []
      };
    },
    methods: {
      // 获取分页数据
      findPage: function (data) {
        this.$refs.CyTable.findPage(this.filters);
      },
      //初始化数据
      init() {
        let that = this;
        //查询所有店铺
        this.utils.request.queryShop({}, res => {
          that.shopList = res.data;
        })
        //查询换货单状态
        this.utils.request.queryUserList(
          {t: "sysConfig", key: "change_product_status"},
          function (data) {
            that.statusList = data.data;
          }
        );
      },
      //选择框
      selectionChange: function (params) {
        this.checked = params.selections
      },
      // 批量删除
      handleDelete: function (data) {
        if (data != null && data.params != null && data.params.length > 0) {
          let ids = data.params.map(item => item.id).toString();

          var params = {};
          params.t = "changeProduct";
          params.ids = ids;
          var this_ = this;
          this.utils.request.deleteUserInfo(params, function (res) {
            if (res.code == "0000") {
              this_.$message({message: "操作成功", type: "success"});
              this_.findPage(null);
            } else {
              this_.$message({message: "操作失败, " + res.msg, type: "error"});
            }
          });
        }
      },
      // 显示换货处理
      handleEdit: function () {
        if (this.checked.length == 1) {
          this.editDialogVisible = true;
          this.dataForm.serveno = this.checked[0].serveno;
          this.dataForm.status = this.checked[0].status;
          this.dataForm.id = this.checked[0].id;
        } else {
          this.$message({message: '退款退货处理必须选中一条数据', type: 'error'})
        }
      },
      // 编辑
      submitForm: function () {
        this.$refs.dataForm.validate(valid => {
          if (valid) {
            this.$confirm("确认提交吗？", "提示", {}).then(() => {
              let params = Object.assign({}, this.dataForm);
              params.t = "changeProduct";
              var this_ = this;
              this.utils.request.editInfo(params, function (res) {
                if (res.code == "0000") {
                  this_.$message({message: "操作成功", type: "success"});

                  this_.$refs["dataForm"].resetFields();

                  this_.editDialogVisible = false;

                  this_.findPage(null);
                } else {
                  this_.$message({
                    message: "操作失败, " + res.msg,
                    type: "error"
                  });
                }
              });
            });
          }
        });
      },

      // 时间格式化
      dateFormat: function (row, column, cellValue, index) {
        return format(row[column.property]);
      },

      resetForm(formName) {
        if (this.$refs[formName] != undefined) {
          this.$refs[formName].resetFields();
          this.filters.start_time = "";
          this.filters.end_time = "";
          this.findPage(null);
        }
      },

      // 处理表格列过滤显示
      displayFilterColumnsDialog: function () {
        this.$refs.tableColumnFilterDialog.setDialogVisible(true);
      },
      // 处理表格列过滤显示
      handleFilterColumns: function (data) {
        this.filterColumns = data.filterColumns;
        this.$refs.tableColumnFilterDialog.setDialogVisible(false);
      },
      // 处理表格列过滤显示
      initColumns: function () {
        let temp = [];

        $.each(this.columns, function (key, val) {
          temp.push(val);
        });
        this.filterColumns = temp;
      },
      //列表下载
      downloadExcel() {
        this.$confirm("确定下载列表文件?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            let filename = "换货管理下载";
            this.$refs.CyTable.exportData(
              this.filters,
              this.filterColumns,
              filename
            );
          })
          .catch(() => {
          });
      }
    },
    mounted() {
      this.initColumns()
      this.init()
    }
  };
</script>

<style>
  .styleReplace .el-form-item__label{
    width:100px !important
  }
  .styleReplace .el-form-item__content{
    margin-left: 100px !important
  }
</style>
